//定义全局变量
var myflag={
    flag:"0",
    div_count:"0",
    allpages:"0",
    currentpage:"1",
    judgeEvent:"0",
    show_title:"",
    asflag:"0",
    saveid:0,//保存当前对象的id
    ff:0
};

//加载所有的帖子
allnotes();
function allnotes(){
    myflag.judgeEvent=1;//判断为所有

    allpages();

    var http,f_data;
    var oall=document.getElementById("all");
    if(window.XMLHttpRequest){
        http=new XMLHttpRequest();
    }else{
        http=ActiveXObject("Microsoft.XMLHTTP");
    }
    http.onreadystatechange=function(){
        if(http.readyState==4&&http.status==200){
            f_data=JSON.parse(http.responseText);
            oall.innerHTML="";
            for(i=0;i<f_data.length;i++){
                oall.innerHTML+="<div class='tiezi'>" +
                    "<img src='"+f_data[i].u_avatar+"'/>" +
                    "<h3>"+f_data[i].n_title+"</h3>" +
                    "<div class='scan'>浏览次数：<span>"+f_data[i].n_count+"</span></div>" +
                    "<div class='time'>发帖时间：<span>"+f_data[i].n_post_time+"</span></div>" +
                    "<div class='answer'>回复：<span>"+f_data[i].n_reply+"</span></div>" +
                    "<span class='yinCang'>"+f_data[i].n_id+"</span></div>";
            }
            //加载所有的当页div
            var arrDiv=document.getElementsByClassName("tiezi");
            //console.log(arrDiv.length);
            for(i=0;i<arrDiv.length;i++){
                arrDiv[i].addEventListener("click",show,false);
            }

        }
    };

    http.open("post","/all_note.do",true);
    http.setRequestHeader("content-type","application/x-www-form-urlencoded");
    http.send("currentpage="+myflag.currentpage);
}
//加载所有开发帖子
function kfnotes(){

    kfallpages();

    var http,f_data;
    var oall=document.getElementById("all");
    if(window.XMLHttpRequest){
        http=new XMLHttpRequest();
    }else{
        http=ActiveXObject("Microsoft.XMLHTTP");
    }
    http.onreadystatechange=function(){
        if(http.readyState==4&&http.status==200){
            f_data=JSON.parse(http.responseText);
            oall.innerHTML="";
            for(i=0;i<f_data.length;i++){
                oall.innerHTML+="<div class='tiezi'>" +
                    "<img src='"+f_data[i].u_avatar+"'/>" +
                    "<h3>"+f_data[i].n_title+"</h3>" +
                    "<div class='scan'>浏览次数：<span>"+f_data[i].n_count+"</span></div>" +
                    "<div class='time'>发帖时间：<span>"+f_data[i].n_post_time+"</span></div>" +
                    "<div class='answer'>回复：<span>"+f_data[i].n_reply+"</span></div>" +
                    "<span class='yinCang'>"+f_data[i].n_id+"</span></div>";
            }
            //加载所有的当页div
            var arrDiv=document.getElementsByClassName("tiezi");
            console.log(arrDiv.length);
            for(i=0;i<arrDiv.length;i++){
                arrDiv[i].addEventListener("click",show,false);
            }

        }
    };
    http.open("post","/kaifa.do",true);
    http.setRequestHeader("content-type","application/x-www-form-urlencoded");
    http.send("currentpage="+myflag.currentpage);
}
//加载所有下载帖子
function xznotes(){

    xzallpages();

    var http,f_data;
    var oall=document.getElementById("all");
    if(window.XMLHttpRequest){
        http=new XMLHttpRequest();
    }else{
        http=ActiveXObject("Microsoft.XMLHTTP");
    }
    http.onreadystatechange=function(){
        if(http.readyState==4&&http.status==200){
            f_data=JSON.parse(http.responseText);
            oall.innerHTML="";
            for(i=0;i<f_data.length;i++){
                oall.innerHTML+="<div class='tiezi'>" +
                    "<img src='"+f_data[i].u_avatar+"'/>" +
                    "<h3>"+f_data[i].n_title+"</h3>" +
                    "<div class='scan'>浏览次数：<span>"+f_data[i].n_count+"</span></div>" +
                    "<div class='time'>发帖时间：<span>"+f_data[i].n_post_time+"</span></div>" +
                    "<div class='answer'>回复：<span>"+f_data[i].n_reply+"</span></div>" +
                    "<span class='yinCang'>"+f_data[i].n_id+"</span></div>";
            }
            //加载所有的当页div
            var arrDiv=document.getElementsByClassName("tiezi");
            console.log(arrDiv.length);
            for(i=0;i<arrDiv.length;i++){
                arrDiv[i].addEventListener("click",show,false);
            }

        }
    };
    http.open("post","/xiazai.do",true);
    http.setRequestHeader("content-type","application/x-www-form-urlencoded");
    http.send("currentpage="+myflag.currentpage);
}
//加载搜索的所有帖子
function searchnotes(){
    var osearchInput=document.getElementById("searchInput");
    var mySearch=osearchInput.value;

    searchpages();

    var http,f_data;
    var oall=document.getElementById("all");
    if(window.XMLHttpRequest){
        http=new XMLHttpRequest();
    }else{
        http=ActiveXObject("Microsoft.XMLHTTP");
    }
    http.onreadystatechange=function(){
        if(http.readyState==4&&http.status==200){
            f_data=JSON.parse(http.responseText);
            oall.innerHTML="";
            for(i=0;i<f_data.length;i++){
                oall.innerHTML+="<div class='tiezi'>" +
                    "<img src='"+f_data[i].u_avatar+"'/>" +
                    "<h3>"+f_data[i].n_title+"</h3>" +
                    "<div class='scan'>浏览次数：<span>"+f_data[i].n_count+"</span></div>" +
                    "<div class='time'>发帖时间：<span>"+f_data[i].n_post_time+"</span></div>" +
                    "<div class='answer'>回复：<span>"+f_data[i].n_reply+"</span></div>" +
                    "<span class='yinCang'>"+f_data[i].n_id+"</span></div>";
            }
            //加载所有的当页div
            var arrDiv=document.getElementsByClassName("tiezi");
            console.log(arrDiv.length);
            for(i=0;i<arrDiv.length;i++){
                arrDiv[i].addEventListener("click",show,false);
            }

        }
    };
    http.open("post","/search.do",true);
    http.setRequestHeader("content-type","application/x-www-form-urlencoded");
    http.send("psearch="+mySearch+"&currentpage="+myflag.currentpage);
}


//点击emoji
var odj_emoji=document.getElementById("dj_emoji");
odj_emoji.addEventListener("click",djEmoji,false);
function djEmoji(e){
    myflag.flag++;
    var oall_emoji=document.getElementById("all_emoji");
    if(myflag.flag==1){
        oall_emoji.style.display="block";
    }else{
        oall_emoji.style.display="none";
        myflag.flag=0;
    }
    stopPropagation(e);
}

//点击添加表情
var oall_emoji=document.getElementById("all_emoji");
var arrEmoji=oall_emoji.getElementsByTagName("img");
var mylen=arrEmoji.length;
for(i=0;i<mylen;i++){
    arrEmoji[i].addEventListener("click",addEmoji,false);
}
function addEmoji(){
    //console.log(this);//this指当前对象
    var oedit=document.getElementById("edit");
    var slt_src=this.getAttribute("src");
    var node=document.createElement("img");
    node.src=slt_src;
    oedit.appendChild(node);
}
//点击发表
var otj=document.getElementById("tj");
otj.addEventListener("click",tj,false);
function tj(){
    var oall=document.getElementById("all");
    var oedit=document.getElementById("edit");
    var otitle=document.getElementById("tz_title");
    var otzT=otitle.getElementsByTagName("input")[0];
    var olei=document.getElementsByName("lei")[0];
    var arroption=olei.getElementsByTagName("option");

    var txt2=oedit.innerHTML;
    var txt1=otzT.value;
    for(i=0;i<arroption.length;i++){
        if(arroption[i].selected==true){
            var myslt=arroption[i].value;
        }
    }
    //判断数据是否为空
    if(txt1==null||txt1==""||txt2==null||txt2==""){
        return ;
    }
    else{

        var data;
        var XHR;
        if(window.XMLHttpRequest){
            XHR=new XMLHttpRequest();
        }else{
            XHR=ActiveXObject("Microsoft.XMLHTTP");
        }
        XHR.onreadystatechange=function(){
            if(XHR.readyState==4&&XHR.status==200){
                data=XHR.responseText;
                if(data=="1"){

                    var http,f_data;
                    if(window.XMLHttpRequest){
                        http=new XMLHttpRequest();
                    }else{
                        http=ActiveXObject("Microsoft.XMLHTTP");
                    }
                    http.onreadystatechange=function(){
                        if(http.readyState==4&&http.status==200){
                            f_data=JSON.parse(http.responseText);
                            //console.log(f_data);
                            //console.log(f_data[0].n_title);
                            var nodeDiv=document.createElement("div");
                            var mynode=oall.firstChild;
                            nodeDiv.innerHTML+="<div class='tiezi'>" +
                                "<img src='"+f_data[0].u_avatar+"'/>" +
                                "<h3>"+f_data[0].n_title+"</h3>" +
                                "<div class='scan'>浏览次数：<span>"+f_data[0].n_count+"</span></div>" +
                                "<div class='time'>发帖时间：<span>"+f_data[0].n_post_time+"</span></div>" +
                                "<div class='answer'>回复：<span>"+f_data[0].n_reply+"</span></div>" +
                                "<span class='yinCang'>"+f_data[0].n_id+"</span></div>";
                            oall.insertBefore(nodeDiv,mynode);

                            //判断帖子溢出,删除节点
                            for(i=0;i<oall.childNodes.length;i++){
                                if(oall.childNodes[i].nodeType==1){
                                    myflag.div_count++;
                                }
                            }
                            if(myflag.div_count>12){
                                var x=oall.lastChild;
                                while(x.nodeType!=1){
                                    x= x.previousSibling;
                                }
                                oall.removeChild(x);
                            }
                            //加载所有的当页div
                            var arrDiv=document.getElementsByClassName("tiezi");
                            console.log(arrDiv.length);
                            for(i=0;i<arrDiv.length;i++){
                                arrDiv[i].addEventListener("click",show,false);
                            }

                        }
                    };
                    http.open("post","/getData.do",true);
                    http.setRequestHeader("content-type","application/x-www-form-urlencoded");
                    http.send();
                }
                else{
                    oLogin.click();
                }
            }
        };
        XHR.open("post","/note.do",true);
        XHR.setRequestHeader("content-type","application/x-www-form-urlencoded");
        XHR.send("ptitle="+txt1+"&plei="+myslt+"&pcontent="+txt2);

        otzT.value="";
        oedit.innerHTML="";
    }

}


//上一页
var oprevious=document.getElementById("previousbtn");
oprevious.addEventListener("click",priviouspage,false);
function priviouspage(){
    myflag.currentpage--;
    var oli2=document.getElementById("li2");
    oli2.value=myflag.currentpage;
    if(myflag.currentpage<1){
        myflag.currentpage++;
        oli2.value=myflag.currentpage;
        alert("这已经是第一页了");
    }
    if(myflag.judgeEvent==3){
        xiaZai();
    }else if(myflag.judgeEvent==2){
        kaifa();
    }else if(myflag.judgeEvent==4){
        searchnotes();
    }else{
        allnotes();
    }

}
//下一页
var onextbtn=document.getElementById("nextbtn");
onextbtn.addEventListener("click",nextpage,false);
function nextpage(){
    myflag.currentpage++;
    var oli2=document.getElementById("li2");
    oli2.value=myflag.currentpage;
    if(myflag.currentpage>myflag.allpages){
        myflag.currentpage--;
        oli2.value=myflag.currentpage;
        alert("这已经是最后一页了");
    }
    //console.log(myflag.judgeEvent);
    if(myflag.judgeEvent==3){
        xznotes();
    }else if(myflag.judgeEvent==2){
        kfnotes();
    }else if(myflag.judgeEvent==4){
        searchnotes();
    }else{
        allnotes();
    }
}



//所有
var oallTieZi=document.getElementById("allTieZi");
oallTieZi.addEventListener("click",allTieZi,false);
function allTieZi(){
    myflag.currentpage=1;
    myflag.judgeEvent=1;//判断为所有
    var oli2=document.getElementById("li2");
    oli2.value=myflag.currentpage;
    allnotes();
}
//开发区
var okaifa=document.getElementById("kaifa");
okaifa.addEventListener("click",kaifa,false);
function kaifa(){
    myflag.currentpage=1;
    var oli2=document.getElementById("li2");
    oli2.value=myflag.currentpage;
    myflag.judgeEvent=2;//判断为开发
    kfnotes();

}
//下载区
var oxiazai=document.getElementById("xiazai");
oxiazai.addEventListener("click",xiaZai,false);
function xiaZai(){
    myflag.currentpage=1;
    var oli2=document.getElementById("li2");
    oli2.value=myflag.currentpage;
    myflag.judgeEvent=3;//判断为下载
    xznotes();
}


//获取总页数
function allpages(){
    var f_data,http;
    if(window.XMLHttpRequest){
        http=new XMLHttpRequest();
    }else{
        http=ActiveXObject("Microsoft.XMLHTTP");
    }
    http.onreadystatechange=function(){
        if(http.readyState==4&&http.status==200){
            f_data=JSON.parse(http.responseText);
            if(f_data[0].pages<=12){
                myflag.allpages=1;
            }else{
                myflag.allpages=Math.ceil(f_data[0].pages/12);
            }
            var oli1=document.getElementById("li1");
            oli1.value=myflag.allpages;
            //console.log(myflag.allpages);
        }
    };
    http.open("post","/allpage.do",true);
    http.setRequestHeader("content-type","application/x-www-form-urlencoded");
    http.send();
}
//开发区所有页
function kfallpages(){
    var f_data,http;
    if(window.XMLHttpRequest){
        http=new XMLHttpRequest();
    }else{
        http=ActiveXObject("Microsoft.XMLHTTP");
    }
    http.onreadystatechange=function(){
        if(http.readyState==4&&http.status==200){
            f_data=JSON.parse(http.responseText);
            if(f_data[0].pages<=12){
                myflag.allpages=1;
            }else{
                myflag.allpages=Math.ceil(f_data[0].pages/12);
            }
            var oli1=document.getElementById("li1");
            oli1.value=myflag.allpages;
            //console.log(myflag.allpages);
        }
    };
    http.open("post","/kfallpage.do",true);
    http.setRequestHeader("content-type","application/x-www-form-urlencoded");
    http.send();
}
//下载区所有页
function xzallpages(){
    var f_data,http;
    if(window.XMLHttpRequest){
        http=new XMLHttpRequest();
    }else{
        http=ActiveXObject("Microsoft.XMLHTTP");
    }
    http.onreadystatechange=function(){
        if(http.readyState==4&&http.status==200){
            f_data=JSON.parse(http.responseText);
            if(f_data[0].pages<=12){
                myflag.allpages=1;
            }else{
                myflag.allpages=Math.ceil(f_data[0].pages/12);
            }
            var oli1=document.getElementById("li1");
            oli1.value=myflag.allpages;
            //console.log(myflag.allpages);
        }
    };
    http.open("post","/xzallpage.do",true);
    http.setRequestHeader("content-type","application/x-www-form-urlencoded");
    http.send();
}
//搜索的所有页数
function searchpages(){
    var osearchInput=document.getElementById("searchInput");
    var mySearch=osearchInput.value;
    var f_data,http;
    if(window.XMLHttpRequest){
        http=new XMLHttpRequest();
    }else{
        http=ActiveXObject("Microsoft.XMLHTTP");
    }
    http.onreadystatechange=function(){
        if(http.readyState==4&&http.status==200){
            f_data=JSON.parse(http.responseText);
            if(f_data[0].pages<=12){
                myflag.allpages=1;
            }else{
                myflag.allpages=Math.ceil(f_data[0].pages/12);
            }
            var oli1=document.getElementById("li1");
            oli1.value=myflag.allpages;
            //console.log(myflag.allpages);
        }
    };
    http.open("post","/searchpage.do",true);
    http.setRequestHeader("content-type","application/x-www-form-urlencoded");
    http.send("psearch="+mySearch);
}

//搜索
var ofaxian=document.getElementById("faxian");
ofaxian.addEventListener("click",faxian,false);
function faxian(){
    myflag.currentpage=1;
    var oli2=document.getElementById("li2");
    oli2.value=myflag.currentpage;
    myflag.judgeEvent=4;//判断为开发
    searchnotes();
    var osearchInput=document.getElementById("searchInput");
    osearchInput.value="";
}

//帖子详情的函数
function show(){
    //console.log(this.childNodes[1])//获取h3标签
    myflag.show_title=this.childNodes[1].innerHTML;
   // console.log(this.getElementsByClassName("yinCang")[0].innerHTML); //当前对象的影藏id
   // var flowName=escape(myflag.show_title);
    myflag.saveid=this.getElementsByClassName("yinCang")[0].innerHTML;
    var http,f_data;
    if(window.XMLHttpRequest){
        http=new XMLHttpRequest();
    }else{
        http=ActiveXObject("Microsoft.XMLHTTP");
    }
    http.onreadystatechange=function(){
        if(http.readyState==4&&http.status==200){
            f_data=JSON.parse(http.responseText);
            console.log(f_data);
            var odiv=document.getElementById("mybg_title_div");
            var oimg=document.getElementById("mybg_title_img");
            var oul=document.getElementById("mybg_reply_ul");
            var oUserName=document.getElementById("user_name");
            var omybg_title=document.getElementById("mybg_title");
            odiv.innerHTML="";
            oul.innerHTML="";
            oUserName.innerHTML="";
            oimg.src="";
            if(f_data.length>=1){//有回复的内容的
                odiv.innerHTML=f_data[0].n_content;
                oimg.src=f_data[0].u_report_avatar;
                oUserName.innerHTML=f_data[0].sname;//发帖人name
                for(i=0;i<f_data.length;i++){
                    oul.innerHTML+="<li><img src='"+f_data[i].u_reply_avatar+"'><p>"+f_data[i].r_content+"</p><span class='otherReplyName'>"+f_data[i].replyname/*回复人name*/+"</span></li>"
                }
            }else{//没有回复内容的
                var _http,_data;
                if(window.XMLHttpRequest){
                    _http=new XMLHttpRequest();
                }else{
                    _http=ActiveXObject("Microsoft.XMLHTTP");
                }
              //  console.log(myflag.show_title)
                _http.onreadystatechange=function(){
                    if(_http.readyState==4&&_http.status==200){
                        _data=_http.responseText
                        _data=JSON.parse(_data);
                        //console.log(_data);
                        odiv.innerHTML=_data[0].n_content;
                        oimg.src=_data[0].u_avatar;
                        oUserName.innerHTML=_data[0].sname;
                        oul.innerHTML="";
                    }
                };
                _http.open("get","/unsendtitle.do?mytitle="+myflag.show_title,true);
                _http.send();
            }

            var omybg=document.getElementById("mybg");
            omybg.style.display="block";
            omybg.addEventListener("click",del,false);
        }
    };
    http.open("get","/sendtitle.do?mytitle="+myflag.show_title,true);
    http.send();

}
function del(){
    var omybg=document.getElementById("mybg");
    //omybg.parentNode.removeChild(omybg);
    omybg.style.display="none";
}
//弹出回复
var opop=document.getElementById("pop");
opop.addEventListener("click",popAs,false);
function popAs(){
    var opopDiv=document.getElementById("mybg_title_popKuang");
    var odiv=opopDiv.getElementsByTagName("div")[0];
    var obtn=opopDiv.getElementsByTagName("button")[0];
    var omybgTitle=document.getElementById("mybg_title");
    if(myflag.asflag==0){
        omybgTitle.style.height="225px";
        omybgTitle.style.transition="height .4s";
        opopDiv.style.height="120px";
        opopDiv.style.border="1px solid grey";
        opopDiv.style.webkitTransition="height .4s";
        odiv.style.height="80px";
        odiv.style.transition="height .4s";
        setTimeout(function(){
            obtn.style.display="block";
        },400);
        myflag.asflag++;
    }else{
        obtn.style.display="none";

        odiv.style.height="0px";
        odiv.style.transition="height .4s";

        opopDiv.style.height="0px";

        opopDiv.style.webkitTransition="height .4s";

        omybgTitle.style.height="100px";
        omybgTitle.style.transition="height .4s";
        setTimeout(function(){
            opopDiv.style.border="none";
        },400);
        myflag.asflag=0;
    }
}
//阻止事件冒泡
function stopPropagation(e) {
    e = e || window.event;
    if(e.stopPropagation) { //W3C阻止冒泡方法
        e.stopPropagation();
    } else {
        e.cancelBubble = true; //IE阻止冒泡方法
    }
}
//阻止冒泡
var omybg_=document.getElementById("mybg_");
omybg_.addEventListener("click",ffous,false);
function ffous(e){
    stopPropagation(e);
}
//点击回复
var opopDiv=document.getElementById("mybg_title_popKuang");
var odiv=opopDiv.getElementsByTagName("div")[0];
var obtn=opopDiv.getElementsByTagName("button")[0];
obtn.addEventListener("click",othersReply,false);
function othersReply(){
    var http,f_data;
    var replyContent=odiv.innerHTML;

    if(window.XMLHttpRequest){
        http=new XMLHttpRequest();
    }else{
        http=ActiveXObject("Microsoft.XMLHTTP");
    }

    http.onreadystatechange=function(){
        if(http.readyState==4&&http.status==200){
            f_data=http.responseText;
            if(f_data=="1"){
                var _http,_data;
                if(window.XMLHttpRequest){
                    _http=new XMLHttpRequest();
                }else{
                    _http=ActiveXObject("Microsoft.XMLHTTP");
                }
                _http.onreadystatechange=function(){
                    if(_http.readyState==4&&_http.status==200){

                        //更新回复的值
                        var xhr;
                        if(window.XMLHttpRequest){
                            xhr=new XMLHttpRequest();
                        }else{
                            xhr=ActiveXObject("Microsoft.XMLHTTP");
                        }
                        xhr.onreadystatechange=function(){
                            if(xhr.readyState==4&&xhr.status==200){
                                if(xhr.responseText=="1"){
                                    myflag.ff=1;
                                    if(myflag.ff==1){
                                        _data=JSON.parse(_http.responseText);
                                        var omybg_reply_ul=document.getElementById("mybg_reply_ul");
                                        var newReplyNode=document.createElement("li");
                                        newReplyNode.innerHTML="<img src='"+_data[0].u_avatar+"'><p>"+_data[0].r_content+"</p>";
                                        omybg_reply_ul.insertBefore(newReplyNode,omybg_reply_ul.firstChild);
                                        myflag.ff=0;
                                    }
                                }
                            }
                        };
                        xhr.open("get","/updatacount.do?ppid="+myflag.saveid,true);
                        xhr.send(null);



                    }
                };
                _http.open("post","/getNewReply.do",true);
                _http.setRequestHeader("content-type","application/x-www-form-urlencoded");
                _http.send();

            }else{
                alert("回复失败");
            }

        }
        odiv.innerHTML="";
    };
    http.open("post","/reply.do",true);
    http.setRequestHeader("content-type","application/x-www-form-urlencoded");
    http.send("pid="+myflag.saveid+"&pcontent="+replyContent);
}

//点击弹出发帖
var ofoot=document.getElementById("foot");
var oreport_open=document.getElementById("report");
oreport_open.addEventListener("click",xsreport,false);
function xsreport(){
    ofoot.style.display="block";
}
document.body.onclick=function(){
    var oallemoji=document.getElementById("all_emoji");
    oallemoji.style.display="none";
};

